package com.river;

import com.river.spark.mr.ScoreVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.*;

/**
 * @author riverfan
 */
public class WordCountDemon {


    public static void main(String[] args) {

        SparkConf conf = new SparkConf();
        conf.setAppName("WordCountDemon");
        //设置master属性
        conf.setMaster("local[2]");
        JavaSparkContext sc = new JavaSparkContext(conf);
        String filePaht = args[0];
        //wordCount1(sc,filePaht);
//        wordCount2(sc,filePaht);

        //wordCount3(sc,filePaht);

    }

    public static void wordCount1(JavaSparkContext sc,String filePaht)
    {

        JavaRDD<String> rdd1 = sc.textFile(filePaht);
        //压扁
        JavaRDD<String> rdd2 = rdd1.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterator<String> call(String s) throws Exception {
                List<String> list = new ArrayList<String>();
                String[] arr = s.split(" ");
                for(String ss : arr){
                    list.add(ss) ;
                }
                return list.iterator() ;
            }
        });
        //映射
        JavaPairRDD<String,Integer> rdd3 = rdd2.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String s) throws Exception {
                return new Tuple2<String, Integer>(s,1);
            }
        });
        //聚合
        JavaPairRDD<String,Integer> rdd4 = rdd3.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        });
        //收集,打印输出
        for(Object o : rdd4.collect()){
            System.out.println(o);
        }
    }

    public static void wordCount2(JavaSparkContext sc,String filePaht){
        int a = 0;
        ScoreVo scoreVo = new ScoreVo();
        scoreVo.setClassName("math");
        scoreVo.setScore(12);
        JavaPairRDD<String,Integer> rdd1 = sc.textFile(filePaht)
                .flatMap( s -> Arrays.asList(s.split(" ")).iterator())
                .mapToPair(s -> {
                    System.out.println(Thread.currentThread());
                    System.out.println(scoreVo);
                    return new Tuple2<>(s, 1);
                })
                .filter(t-> StringUtils.isNoneBlank(t._1) && !StringUtils.equals(t._1,","))
                .reduceByKey((v1,v2)-> (v1+v2));
        rdd1.collect().forEach(t-> System.out.println(t + "  " + Thread.currentThread()));

    }

    //单词统计-纺织系数据倾斜
    public static void wordCount3(JavaSparkContext sc,String filePaht){
        JavaPairRDD<String,Integer> rdd1 = sc.textFile(filePaht)
                .flatMap( s -> Arrays.asList(s.split(" ")).iterator())
                .filter(t->StringUtils.isNoneBlank(t))
                .mapToPair(s -> new Tuple2<>(s+"_"+RandomUtils.nextInt(0,100), 1))
                .reduceByKey((v1,v2)-> (v1+v2))
                .mapToPair(t-> new Tuple2<>(t._1.substring(0,t._1.indexOf('_')), t._2))
                .reduceByKey((v1,v2)-> (v1+v2));
        rdd1.collect().forEach(t-> System.out.println(t + " " + Thread.currentThread()));
    }


}
